package com.flyme.module.base.provider.service.impl;

import com.flyme.common.core.model.query.CriteriaQuery;
import com.flyme.common.core.model.query.PagerInfo;
import com.flyme.common.core.model.query.PqGrid;
import com.flyme.core.base.supper.entity.EntityMap;
import com.flyme.core.base.supper.service.impl.BaseServiceImpl;
import com.flyme.module.base.client.entity.SysIntegralrecord;
import com.flyme.module.base.provider.mapper.SysIntegralrecordMapper;
import com.flyme.module.base.provider.service.SysIntegralrecordService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author: zyf
 * @date: 2018/10/24 16:33
 * @description:
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class SysIntegralrecordServiceImpl extends BaseServiceImpl<SysIntegralrecordMapper, SysIntegralrecord> implements SysIntegralrecordService {

    /**
     * 根据用户查询积分记录
     */
    public List<EntityMap> selectByUser(String accountId, PagerInfo pagerInfo){
        pagerInfo.setPageSize(20);
        CriteriaQuery queryWrapper = new CriteriaQuery<EntityMap>(pagerInfo);
        queryWrapper.select("*");
        queryWrapper.eq("accountId", accountId);
        queryWrapper.orderByDesc("createTime");
        return (List<EntityMap>) pageList(queryWrapper).getRows();
    }


    /**
     * 网站根据用户查询积分记录
     */
    public PqGrid selectByUserWeb(String accountId, PagerInfo pagerInfo){
        CriteriaQuery queryWrapper = new CriteriaQuery<EntityMap>(pagerInfo);
        queryWrapper.select("*");
        queryWrapper.eq("accountId", accountId);
        queryWrapper.orderByDesc("createTime");
        return pageList(queryWrapper);
    }

    /**
     * 根据用户查询积分记录
     */
    public PqGrid selectByUser2(String accountId, PagerInfo pagerInfo){
        CriteriaQuery queryWrapper = new CriteriaQuery<EntityMap>(pagerInfo);
        queryWrapper.select("*");
        queryWrapper.eq("accountId", accountId);
        queryWrapper.orderByDesc("createTime");
        return pageList(queryWrapper);
    }

    /**
     * 根据日期模糊查询当天是否有分享过
     */
    public SysIntegralrecord findByDate(String date, Long userId){
        return getOne(q().like(true, "createTime", date).eq(true, "accountId", userId).like("recordCode", "scoreShare"), false);
    }

    /**
     * 统计用户分享次数
     */
    public Integer countShare(Long userId){
        return count(q().eq("accountId", userId).like("recordBody", "分享"));
    }

}
